package com.ip.pomodoro.model

class ActivityInventoryController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
        [activityInventoryInstanceList: ActivityInventory.list(params), activityInventoryInstanceTotal: ActivityInventory.count()]
    }

    def create = {
        def activityInventoryInstance = new ActivityInventory()
        activityInventoryInstance.properties = params
        return [activityInventoryInstance: activityInventoryInstance]
    }

    def save = {
        def activityInventoryInstance = new ActivityInventory(params)
        if (activityInventoryInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'activityInventory.label', default: 'ActivityInventory'), activityInventoryInstance.id])}"
            redirect(action: "show", id: activityInventoryInstance.id)
        }
        else {
            render(view: "create", model: [activityInventoryInstance: activityInventoryInstance])
        }
    }

    def show = {
        def activityInventoryInstance = ActivityInventory.get(params.id)
        if (!activityInventoryInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'activityInventory.label', default: 'ActivityInventory'), params.id])}"
            redirect(action: "list")
        }
        else {
            [activityInventoryInstance: activityInventoryInstance]
        }
    }

    def edit = {
        def activityInventoryInstance = ActivityInventory.get(params.id)
        if (!activityInventoryInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'activityInventory.label', default: 'ActivityInventory'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [activityInventoryInstance: activityInventoryInstance]
        }
    }

    def update = {
        def activityInventoryInstance = ActivityInventory.get(params.id)
        if (activityInventoryInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (activityInventoryInstance.version > version) {
                    
                    activityInventoryInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'activityInventory.label', default: 'ActivityInventory')] as Object[], "Another user has updated this ActivityInventory while you were editing")
                    render(view: "edit", model: [activityInventoryInstance: activityInventoryInstance])
                    return
                }
            }
            activityInventoryInstance.properties = params
            if (!activityInventoryInstance.hasErrors() && activityInventoryInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'activityInventory.label', default: 'ActivityInventory'), activityInventoryInstance.id])}"
                redirect(action: "show", id: activityInventoryInstance.id)
            }
            else {
                render(view: "edit", model: [activityInventoryInstance: activityInventoryInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'activityInventory.label', default: 'ActivityInventory'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def activityInventoryInstance = ActivityInventory.get(params.id)
        if (activityInventoryInstance) {
            try {
                activityInventoryInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'activityInventory.label', default: 'ActivityInventory'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'activityInventory.label', default: 'ActivityInventory'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'activityInventory.label', default: 'ActivityInventory'), params.id])}"
            redirect(action: "list")
        }
    }
}
